
#include <stdio.h>
#include <stdlib.h>
#include "streamdata.h"


int streamdatainit(STREAMDATA *streamdata, int cachesize, int conditionsize)
{
    cachesize = cachesize < 32 ? 32 : cachesize;
    conditionsize = conditionsize < 1 ? 1 : conditionsize;
    streamdata->data = (char *)calloc(cachesize, 1);
    if (streamdata->data == NULL)
    {
        // 初始化失败
        return -1;
    }
    streamdata->size = cachesize;
    streamdata->conditions = (CONDITION*)calloc(conditionsize, sizeof(CONDITION) / sizeof(char));
    if (streamdata->conditions == NULL)
    {
        // 初始化失败
        return -1;
    }
    streamdata->conditionsize = conditionsize;
}

int streamdataaddhandle(STREAMDATA *streamdata, HANDLER handler)
{
    streamdata->handler = handler;
}
int streamdataaddcondition(STREAMDATA *streamdata, CONDITION condition)
{
    int i = 0;
    for (i = 0; i < streamdata->conditionsize; i++)
    {
        if (streamdata->conditions[i] == 0)
        {
            streamdata->conditions[i] = condition;
            return 0;
        }
        else
        {
            continue;
        }
    }
    if (i == streamdata->conditionsize)
    {
        return -1;
    }
}

int streamdataadddata(STREAMDATA *streamdata, char *data, int length)
{
    if (streamdata->size == 0)
    {
        return -1;
    }
    if ((streamdata->insertindex + length) > streamdata->size)
    {
        // 越界
        return -1;
    }
    for (int i = 0; i < length; i++)
    {
        streamdata->data[streamdata->insertindex] = data[i];
        streamdata->insertindex++;
    }
}

int streamdatahandle(STREAMDATA *streamdata)
{
    if (streamdata->handler == 0)
    {
        return 0;
    }

    while (1)
    {
        int bfh = 0;
        for (int i = 0; i < streamdata->conditionsize; i++)
        {
            if (streamdata->conditions[i] != 0)
            {
                if (streamdata->conditions[i](streamdata,&streamdata->data[streamdata->readindex]) < 0)
                {
                    bfh = 1;
                }
            }
            else
            {
                continue;
            }
        }
        if(bfh==1)
        {
            //不符合判断条件，移动一个字节
            streamdata->readindex++;
            if(streamdata->readindex==streamdata->size)
            {
                //查找完成，没有指令
                streamdata->readindex=0;
                return 0;
            }
            continue;
        }
        else
        {
            //符合所有判断条件，解析指令
            int count=streamdata->handler(streamdata,&streamdata->data[streamdata->readindex]); //解析
            streamdata->count++;
            //前移
            for(int i=0;(streamdata->readindex+count+i)<streamdata->size;i++)
            {
                streamdata->data[i]=streamdata->data[streamdata->readindex+count+i];
            }
            if(streamdata->readindex>0)
            {
                //有忽略的字节
                streamdata->ignorecount+=streamdata->readindex;
            }
            streamdata->insertindex=streamdata->insertindex-count-streamdata->readindex;
            streamdata->readindex=0;

        }
    }
}